/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.tika.config;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * Global Tika configuration settings that don't belong to specific components.
 * These are top-level settings in the JSON configuration file.
 *
 * <p>Example JSON:
 * <pre>
 * {
 *   "maxJsonStringFieldLength": 50000000,
 *   "xml-reader-utils": {
 *     "maxEntityExpansions": 1000,
 *     "maxNumReuses": 100,
 *     "poolSize": 10
 *   }
 * }
 * </pre>
 */
public class GlobalSettings {

    /**
     * Static maximum length for JSON string fields.
     * Default: 20,000,000 (Jackson's default)
     * This is static because it's a global setting that affects all JSON parsing.
     */
    private static Integer maxJsonStringFieldLength = 20_000_000;

    /**
     * Instance field for deserialization from JSON.
     * The value is copied to the static field when set.
     */
    @JsonProperty("maxJsonStringFieldLength")
    private Integer instanceMaxJsonStringFieldLength = 20_000_000;

    /**
     * Service loader configuration for handling initialization problems.
     */
    @JsonProperty("service-loader")
    private ServiceLoaderConfig serviceLoader;

    /**
     * XML reader utilities configuration for security limits.
     */
    @JsonProperty("xml-reader-utils")
    private XmlReaderUtilsConfig xmlReaderUtils;

    /**
     * Gets the static maximum JSON string field length.
     *
     * @return the max length, or null if not set
     */
    public static Integer getMaxJsonStringFieldLength() {
        return maxJsonStringFieldLength;
    }

    /**
     * Sets the static maximum JSON string field length.
     * This affects all JSON parsing globally.
     *
     * @param length the max length to set
     */
    public static void setMaxJsonStringFieldLength(Integer length) {
        maxJsonStringFieldLength = length;
    }

    /**
     * Instance getter for deserialization.
     * Returns the instance value which may differ from the static value.
     *
     * @return the instance max length
     */
    public Integer getInstanceMaxJsonStringFieldLength() {
        return instanceMaxJsonStringFieldLength;
    }

    /**
     * Instance setter for deserialization.
     * Automatically updates the static field when set.
     *
     * @param length the max length to set
     */
    public void setInstanceMaxJsonStringFieldLength(Integer length) {
        this.instanceMaxJsonStringFieldLength = length;
        if (length != null) {
            setMaxJsonStringFieldLength(length);
        }
    }

    public ServiceLoaderConfig getServiceLoader() {
        return serviceLoader;
    }

    public void setServiceLoader(ServiceLoaderConfig serviceLoader) {
        this.serviceLoader = serviceLoader;
    }

    public XmlReaderUtilsConfig getXmlReaderUtils() {
        return xmlReaderUtils;
    }

    public void setXmlReaderUtils(XmlReaderUtilsConfig xmlReaderUtils) {
        this.xmlReaderUtils = xmlReaderUtils;
    }

    /**
     * Service loader configuration.
     */
    public static class ServiceLoaderConfig {
    }

    /**
     * XML reader utilities security configuration.
     */
    public static class XmlReaderUtilsConfig {
        /**
         * Maximum entity expansions allowed in XML parsing.
         */
        @JsonProperty("maxEntityExpansions")
        private Integer maxEntityExpansions;

        /**
         * Maximum number of times an XML reader can be reused from the pool.
         */
        @JsonProperty("maxNumReuses")
        private Integer maxNumReuses;

        /**
         * Size of the XML reader pool.
         */
        @JsonProperty("poolSize")
        private Integer poolSize;

        public Integer getMaxEntityExpansions() {
            return maxEntityExpansions;
        }

        public void setMaxEntityExpansions(Integer maxEntityExpansions) {
            this.maxEntityExpansions = maxEntityExpansions;
        }

        public Integer getMaxNumReuses() {
            return maxNumReuses;
        }

        public void setMaxNumReuses(Integer maxNumReuses) {
            this.maxNumReuses = maxNumReuses;
        }

        public Integer getPoolSize() {
            return poolSize;
        }

        public void setPoolSize(Integer poolSize) {
            this.poolSize = poolSize;
        }
    }
}
